#pragma once

#ifdef SLOW_IO_BY_JUMPING
#define __SLOW_DOWN_IO __asm__ __volatile__("jmp 1f\n1:\tjmp 1f\n1:")
#else
#define __SLOW_DOWN_IO __asm__ __volatile__("outb %al,$0x80")
#endif

#ifdef REALLY_SLOW_IO
#define SLOW_DOWN_IO \
    { \
        __SLOW_DOWN_IO; \
        __SLOW_DOWN_IO; \
        __SLOW_DOWN_IO; \
        __SLOW_DOWN_IO; \
    }
#else
#define SLOW_DOWN_IO __SLOW_DOWN_IO
#endif

#define __OUT1(s, x) \
    static inline void __out##s(unsigned x value, unsigned short port) {

#define __OUT2(s, s1, s2) \
__asm__ __volatile__ ("out" #s " %" s1 "0,%" s2 "1"

#define __OUT(s, s1, x) \
__OUT1(s,x) __OUT2(s,s1,"w") : : "a" (value), "d" (port)); \
    } \
__OUT1(s##c,x) __OUT2(s,s1,"") : : "a" (value), "i" (port)); \
    } \
__OUT1(s##_p,x) __OUT2(s,s1,"w") : : "a" (value), "d" (port)); \
    SLOW_DOWN_IO; \
    } \
__OUT1(s##c_p,x) __OUT2(s,s1,"") : : "a" (value), "i" (port)); \
    SLOW_DOWN_IO; \
    }

__OUT(b, "b", char)

#define outb_p(val, port) \
    ((__builtin_constant_p((port)) && (port) < 256) ? __outbc_p((val), (port)) : __outb_p((val), (port)))
